<?php
namespace app\api\controller\v1;

use app\admin\model\Wxuser;
use app\common\controller\JwtTools;
use app\common\model\Config;
use EasyWeChat\Factory;
use think\Cache;
use think\cache\driver\Redis;
use think\Env;
use think\Session;

class Login extends Base
{

    //用Code换信息
    public function codeChangeMessage()
    {
        if(empty($this->params['code'])){$this->error('缺少必要参数');}


        if(Env::get('wxmini.debug'))
        {
            $config             = [
                'app_id' => Env::get('wxmini.debug_app_id'),
                'secret' => Env::get('wxmini.debug_app_secret'),
                'response_type' => 'array',
            ];
        }else{
            $config             = [
                'app_id' => Env::get('wxmini.true_app_id'),
                'secret' => Env::get('wxmini.true_app_secret'),
                'response_type' => 'array',
            ];
        }

        $app                = Factory::miniProgram($config);

        $content            = '';

        try{

            $content        = $app->auth->session($this->params['code']);

        }catch (\Exception $e){

            $this->error($e->getMessage());
        }

        $this->success('登陆成功',$content);

    }


    //登录
    public function login()
    {

        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_openid|OpenId', 'fk_bdl_phone|手机号','phone_cms|验证码'],
            ['require',  'require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);

        }

        //校验验证码
        $code = cache($this->params['fk_bdl_phone']);

//        if($this->params['phone_cms'] !== "666666")
//        {
            if($code != $this->params['phone_cms'])
            {
                 $this->error('验证码错误');
            }
//        }


        //查询用户是否存在
        $userInfo = Wxuser::where('fk_bdl_phone', $this->params['fk_bdl_phone'])->find();



        if($userInfo['fenable'] == 0)
        {
            $this->result('用户已被禁用',[],504);
        }


        $is_perfect = 2;

        if(!$userInfo)
        {
            $userCreateData = [
                'fk_bdl_phone'      => $this->params['fk_bdl_phone'],
                'fk_bdl_openid'     => $this->params['fk_bdl_openid'],
            ];

            $res            = Wxuser::create($userCreateData);

            if($res)
            {
                $userID     = $res->id;

            }else{
                $this->error('请稍后重试');
            }
        }else{

            if($userInfo['fk_bdl_reqcompanyname'] !== ' ' && $userInfo['fk_bdl_requsername'] !== ' ' && $userInfo['fk_bdl_reqaddress'] !== ' '&& $userInfo['fk_bdl_reqphone'] !== ' '&& $userInfo['fk_bdl_reqattachurl'] !== ' ')
            {
                $is_perfect = 1;
            }

            $userID = $userInfo->id;

            Wxuser::where('id',$userID)->update(['fk_bdl_openid' => $this->params['fk_bdl_openid']]);

        }

        $jwt      = new JwtTools();

        $token = $jwt->token($userID);
        $data = [
            'is_perfect' => $is_perfect,
            'token'     => $token
        ];

        Wxuser::where('id',$userID)->update(['fk_bdl_token' => $token]);

        Cache::rm($this->params['fk_bdl_phone']);
        $this->success('登录成功',$data);


    }

    //拿到协议
    public function getService()
    {
        $check      = $this->plulicValidate($this->params,
            ['field'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $data = Config::where('name',$this->params['field'])->value('value');


        $this->success('',$data);
    }
    public function sendCms()
    {
        $check      = $this->plulicValidate($this->params,
            ['phone'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }
//        请联系业务员开通账号
        //查询用户是否存在
        $userInfo = Wxuser::where('fk_bdl_phone', $this->params['phone'])->find();

        if(empty($userInfo))
        {
            $this->error('请联系业务员开通账号');
        }


        if($userInfo['fenable'] == 0)
        {
            $this->error('用户已被禁用');
        }



        $code   = rand(100000, 999999);
        $res    = sendCms($this->params['phone'], $code);
        if($res)
        {
            cache($this->params['phone'], $code, 6000);
            $this->success('发送成功');
        }else{
            $this->error('稍后再试');
        }
    }


}